package bigbeard.tools.thread.pools;

import java.util.concurrent.*;

/**
 * @author by bigbeard on 2018/9/14.
 * <p>
 * 线程池创建工具类,建议所有的需要用到线程的地方都通过此方法进行创建,
 * 不论是否为线程池.
 */
public class ThreadPoolManager {

    /**
     * 创建一个线程池
     *
     * @param corePoolSize  　核心线程数量
     * @param maxPoolSize   　最大线程数量
     * @param keepAliveTime 　非核心线程的空置时间,单位:豪秒
     * @param workQueue     　排队队列
     * @param threadFactory 　线程创建信息;
     * @param handler       当超排队队列时采取的策略,此处可以将无法入对的信息进行独立处理
     * @return 线程池
     */
    protected ExecutorService createExecutorService(
            int corePoolSize,
            int maxPoolSize,
            long keepAliveTime,
            BlockingQueue<Runnable> workQueue,
            ThreadFactory threadFactory,
            RejectedExecutionHandler handler) {

        return new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                                      keepAliveTime, TimeUnit.MILLISECONDS,
                                      workQueue, threadFactory,
                                      handler);
    }

    /**
     * 创建线程池,当超过排队满后系统将拒绝进入队列,同时发出拒绝入队的异常
     *
     * @param corePoolSize  　核心线程数量
     * @param maxPoolSize   　最大线程数量
     * @param keepAliveTime 　非核心线程的空置时间,单位:豪秒
     * @param workQueue     　排队队列
     * @param threadFactory 　线程创建信息;
     * @return 线程池
     */
    protected ExecutorService createExecutorService(
            int corePoolSize,
            int maxPoolSize,
            long keepAliveTime,
            BlockingQueue<Runnable> workQueue,
            ThreadFactory threadFactory) {

        return createExecutorService(corePoolSize, maxPoolSize, keepAliveTime, workQueue, threadFactory
                , new ThreadPoolExecutor.AbortPolicy());

    }

    /**
     * 创建线程池,当超过排队满后系统将拒绝进入队列,同时发出拒绝入队的异常
     *
     * @param corePoolSize  　核心线程数量
     * @param maxPoolSize   　最大线程数量
     * @param workQueue     　排队队列
     * @param threadFactory 　线程创建信息;
     * @return 线程池
     */
    protected ExecutorService createExecutorService(
            int corePoolSize,
            int maxPoolSize,
            BlockingQueue<Runnable> workQueue,
            ThreadFactory threadFactory) {
        return createExecutorService(corePoolSize, maxPoolSize, 0L, workQueue, threadFactory
                , new ThreadPoolExecutor.AbortPolicy());
    }

    /**
     * 定时执行任务
     *
     * @param corePoolSize  核心线程数量
     * @param task          任务
     * @param interval      间隔时间
     * @param unit          时间单位
     * @param threadFactory 线程构造器
     * @param handle        超过线程最大容量之后的拒绝处理
     * @return 定时构造期
     */
    protected ScheduledExecutorService createSchedule(
            int corePoolSize,
            Runnable task,
            int interval,
            TimeUnit unit,
            ThreadFactory threadFactory,
            RejectedExecutionHandler handle) {
        ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(corePoolSize, threadFactory, handle);
        s.schedule(task, interval, unit);
        return s;
    }

    /**
     * 定时执行任务
     *
     * @param corePoolSize  核心线程数量
     * @param task          任务
     * @param interval      间隔时间
     * @param unit          时间单位
     * @param threadFactory 线程构造器
     * @param handle        超过线程最大容量之后的拒绝处理
     * @return 定时构造期
     */
    protected <V> ScheduledExecutorService createSchedule(
            int corePoolSize,
            Callable<V> task,
            int interval,
            TimeUnit unit,
            ThreadFactory threadFactory,
            RejectedExecutionHandler handle) {
        ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(corePoolSize, threadFactory, handle);
        s.schedule(task, interval, unit);
        return s;
    }
}
